Balancing glasses on a plate


This application is a glass balancing game with up to four participants. It demonstrates multiple users, and uses the accelerometer in the Wiimote. The idea of the game is to balance the Wiimote on the hand similar to how a waiter may carry a plate of drinks. Tilting the plate too far will cause the drinks to fall off. Press the A button on the Wiimote to load each tray.

Here is the original flash file with comments - balance.fla. Below you will find the code and the comments:

Frame 1:


//This script was explained in earlier examples
var numberOfWiimotes:Number
stop()
 
 
for(var wiis:int = 1; wiis<=4; wiis++){
    this["wii_"+wiis+"_mc"].addEventListener(MouseEvent.MOUSE_UP, setWiiMoteCount)
    this["wii_"+wiis+"_mc"].moteAmount = wiis
    this["wii_"+wiis+"_mc"].buttonMode = true
}
 
function setWiiMoteCount(event:MouseEvent):void{
    numberOfWiimotes = event.currentTarget.moteAmount
    gotoAndStop(2)
}

Frame 2:


//This script was explained in earlier examples
import org.wiiflash.Wiimote;
import org.wiiflash.events.ButtonEvent;
import org.wiiflash.events.WiimoteEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*
import flash.events.*;
 
 
//This script was explained in earlier examples
for(var i:int = 0; i< numberOfWiimotes; i++){
 
    //This script was explained in earlier examples
    var myWiimote:Wiimote = new Wiimote();
    myWiimote.connect ();
    myWiimote.addEventListener( Event.CONNECT, onWiimoteConnect );
}
 
function onWiimoteConnect ( pEvent:Event ):void
 
{
        //This script was explained in earlier examples
        pEvent.target.addEventListener( ButtonEvent.A_RELEASE, onAReleased);
        pEvent.target.addEventListener( WiimoteEvent.UPDATE, onUpdated);
 
        //variable that is used to detect whether a glass is animating (falling off the plate)
        this["glass_"+pEvent.target.id+"_mc"].tweening = true
}
// handling buttons
function onAReleased (pEvt:ButtonEvent ):void
 
{
    //All Wiimotes initially call this function when their A button is pressed, this will set the glass on to the plate.
    startGlass(this["glass_"+ pEvt.target.id+"_mc"])
 
}
 
//Tween variables that are used to create the falling glass if the plate is tipped to far
var tweenDropLeftX:Tween
var    tweenDropLeftY:Tween
var tweenDropRightX:Tween
var    tweenDropRightY:Tween
var removeMaskTween:Tween
 
function startGlass(obj:Object){
 
    //obj is the glass that the Wiimote is communicating with.
    //below sets the glass to the same position as the center of the plate.
    obj.glassmask_mc.x = -10
    obj.glassmask_mc.y = 165.4
    obj.glass_mc.x = 0
    obj.glass_mc.y =165.4
    obj.tweening = false
}
 
function startTweenLeft(obj:Object){
    //Animation used to make it look like the glass has fallen off when the plate is tipped to far to the LEFT.
    tweenDropLeftX = new Tween(obj.glass_mc, "x", Strong.easeInOut, obj.glass_mc.x, -500, 1, true)
    tweenDropLeftY = new Tween(obj.glass_mc, "y", Strong.easeInOut, obj.glass_mc.y, 2000, 1, true)
    removeMaskTween = new Tween(obj.glassmask_mc, "x", Strong.easeInOut, obj.glassmask_mc.x, -3000, .5, true)
}
 
function startTweenRight(obj:Object){
    //Animation used to make it look like the glass has fallen off when the plate is tipped to far to the RIGHT.
    tweenDropRightX = new Tween(obj.glass_mc, "x", Strong.easeInOut, obj.glass_mc.x, 680, 1, true)
    tweenDropRightY = new Tween(obj.glass_mc, "y", Strong.easeInOut, obj.glass_mc.y, 2000, 1, true)
    removeMaskTween = new Tween(obj.glassmask_mc, "x", Strong.easeInOut, obj.glassmask_mc.x, 3000,.8, true)
}
 
function onUpdated (pEvt:WiimoteEvent ):void
 
{
    //The onUpdated funciton is similar to (the same as..) an EnterFrame event it is called when the Wiimote updates (every frame...)
 
    //setting the obj variable to the current glass.
    var obj:Object = this["glass_"+pEvt.target.id+"_mc"]
 
    //set theNum, which relates to the current pitch of the Wiimote. To... a bunch of numbers... essentially this helps the
    //program to match the rotation of the Wiimote in reality.
    obj.theNum = (pEvt.target.pitch * 100) /1.80
 
    //if theNum is between 5 and -5 AND is currently falling off...
    if(obj.theNum > -5 && obj.theNum < 5 && !obj.tweening){
        //...run this part, which is set up to stop excessive shake.
        obj.glassmask_mc.rotation = obj.glass_mc.rotation = obj.plate_mc.rotation = 0
 
    }else{
        //else do the rotation and match it to the current pitch of the Wiimote.
        obj.glassmask_mc.rotation = obj.glass_mc.rotation = obj.plate_mc.rotation = Number(obj.theNum.toPrecision(2))
    }
 
    //if the plate is tipped to far to the LEFT the start the tweening (falling off animation)
    if(obj.glass_mc.rotation <-15 && !obj.tweening){
        obj.tweening = true
        startTweenLeft(obj)
    }
    //if the plate is tipped to far to the RIGHT the start the tweening (falling off animation)
    if(obj.glass_mc.rotation >15 && !obj.tweening){
        obj.tweening = true
        startTweenRight(obj)
    }
}